iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 23
1

今天逆向並找出指標的特徵。指標意即記憶體位置,系統在傳資料的時候大多使用指標進行資料傳遞。

指標

disassemble main

跟上圖大同小異。

解答公布:

#include<stdio.h>

int main(){
    int box = 10;
    int *boxPtr;
    boxPtr = &box;
    printf("Address of box is %p\n", &box);
    printf("Address of boxPtr is %p\n", &boxPtr);
    printf("Address stored in  boxPtr is %p\n", boxPtr);
    printf("Value of box is %d\n", box);
    printf("Value of *boxPtr is %d\n", *boxPtr);
    return 0;
}

指標的指標/指標的指標的指標

disassemble main

很長一段(如下圖):
簡單來說,就是:cPtr -> bPtr -> aPtr -> a

解答公布:

#include<stdio.h>

int main(){

  int a = 16;
  int *aPtr;
  aPtr = &a;
  printf("Address of a is %p\n", &a);
  printf("Address of aPtr is %p\n", &aPtr);
  printf("Address stored in  aPtr is %p\n", aPtr);
  printf("Value of a is %d\n", a);
  printf("Value of *aPtr is %d\n", *aPtr);
  printf("\n");

  int **bPtr;
  bPtr = &aPtr;
  printf("Address of bPtr is %p\n", &bPtr);
  printf("Address stored in  bPtr is %p\n", bPtr);
  printf("Value of **&aPtr is %d\n", **&aPtr);
  printf("Value of **bPtr is %d\n", **bPtr);
  printf("\n");

  int ***cPtr;
  cPtr = &bPtr;
  printf("Address of cPtr is %p\n", &cPtr);
  printf("Address stored in cPtr is %p\n", cPtr);
  printf("Value of ***&bPtr is %d\n", ***&bPtr);
  printf("Value of ***cPtr is %d\n", ***cPtr);
  printf("\n");

  printf("Address of cPtr is %p\n", &cPtr);
  printf("Address stored in cPtr is %p\n", cPtr);
  printf("Address of bPtr is %p\n", &bPtr);
  printf("Address stored in bPtr is %p\n", bPtr);
  printf("Address of aPtr is %p\n", &aPtr);
  printf("Address stored in aPtr is %p\n", aPtr);
  printf("Address of a is %p\n", &a);
  printf("Value stored in a is %d\n", a);
  return 0;

}

記憶體配置與釋放

disassemble main

下圖的 call 0x10b0 就是呼叫 malloc()

解答公布:

#include<stdio.h>

int main(){
    int *aPtr = (int *)malloc(sizeof(int));
    *aPtr = 16;

        printf("The Address is :%p\n", &aPtr);
        printf("Address stored in aPtr is %p\n", aPtr);
        printf("The value is : %d\n", *aPtr);

        free(aPtr);
    return 0;
}

結論

指標就是記憶體的位址。明天接著介紹應用!


上一篇
Day22 - 逆向並找出陣列的特徵
下一篇
Day24 - 一題入門的 CTF
系列文
逆向工程 – 從入門到放棄30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言